package com.example.ui.homepage;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.MyMoveView;
import com.example.huashangapp.R;
import com.example.net.Communication;
import com.example.service.MainService;
import com.example.ui.MainActivity;
import com.example.ui.leftmenu.LeftMenuView;
import com.example.ui.leftmenu.MyListView;
import com.example.util.AsyncLoadImage;
import com.example.util.BitmapLoad;

public class MyInformationActivity extends Activity implements  RadioGroup.OnCheckedChangeListener,CompoundButton.OnCheckedChangeListener{
	private MyInformationActivity context;
	private Button titleLeftBtn,titleRightBtn;
	private TextView textTitle;
	public static ImageView imgHead;
	private TextView myName;
	public static TextView companyText,zhiwuText,occupationText,needText,supplyText,sexText;
	private EditText companyEdit,zhiwuEdit,occupationEdit,needEdit,supplyEdit;
	private ToggleButton rightBtn;
//	private LinearLayout sexLayout;
	private RadioGroup sexRadio;
	private RadioButton sex_man,sex_woman;
	private String company,zhiwu,occupation,need,supply,sex;
//	public static boolean isUpdate;
	public static Handler handler;
	private String headPath;
	private Intent myIntent;
	private  String myId;
	public MyMoveView myMoveView;
	private AsyncLoadImage asyncLoadImage;
	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		context = this;
		asyncLoadImage=new AsyncLoadImage();
		myIntent = getIntent();
		headPath=HSApplication.sp.getString("headPath", "");
		myId = myIntent.getStringExtra("myId");
		myMoveView=MainActivity.getMainActivityContext();
		setContentView(R.layout.my_information);
		findView();
		setClick();
		
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					
					sexRadio.setVisibility(View.GONE);
					companyEdit.setVisibility(View.GONE);
					zhiwuEdit.setVisibility(View.GONE);
					occupationEdit.setVisibility(View.GONE);
					needEdit.setVisibility(View.GONE);
					supplyEdit.setVisibility(View.GONE);

					companyText.setVisibility(View.VISIBLE);
					zhiwuText.setVisibility(View.VISIBLE);
					occupationText.setVisibility(View.VISIBLE);
					needText.setVisibility(View.VISIBLE);
					supplyText.setVisibility(View.VISIBLE);
					
					companyText.setText(company);
					zhiwuText.setText(zhiwu);
					occupationText.setText(occupation);
					needText.setText(need);
					supplyText.setText(supply);
					
					HSApplication.sp.getString("gender","");//性别
					HSApplication.sp.getString("company","");//公司
					HSApplication.sp.getString("zhiwu","");//职务
					HSApplication.sp.getString("qccupation","");//行业
					HSApplication.sp.getString("requirement","");//需求
					HSApplication.sp.getString("supply","");//供应
					break;
				
				default:
					break;
				}
				super.handleMessage(msg);
			}
		};
		
	}
 Handler head=new Handler(){
	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case 1:
			asyncLoadImage.imageLoader.displayImage(headPath, imgHead,asyncLoadImage.options);
			break;
		default:
			break;
		}
		super.handleMessage(msg);
	}
	
};

	private void findView() {
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		rightBtn = (ToggleButton)findViewById(R.id.layout_title_right_togglebtn);
		textTitle=(TextView)findViewById(R.id.layout_title_middle_text);
		
		
	
		myName=(TextView)findViewById(R.id.my_information_name);
		sexText = (TextView)findViewById(R.id.my_information_sex);
		sexRadio=(RadioGroup)findViewById(R.id.my_information_sex_radiogroup);
		sex_man = (RadioButton)findViewById(R.id.my_information_sex_man);
		sex_woman = (RadioButton)findViewById(R.id.my_information_sex_women);
		
		companyText=(TextView)findViewById(R.id.my_information_company_textview);//公司
		zhiwuText=(TextView)findViewById(R.id.my_information_department_textview);//职务
		occupationText=(TextView)findViewById(R.id.my_information_post_textview);//行业
		needText=(TextView)findViewById(R.id.my_information_industry_textview);//需求
		supplyText=(TextView)findViewById(R.id.my_information_supply_textview);//供应

		companyEdit=(EditText)findViewById(R.id.my_information_company_edittext);//公司
		zhiwuEdit=(EditText)findViewById(R.id.my_information_department_edittext);//职务
		occupationEdit=(EditText)findViewById(R.id.my_information_post_edittext);//行业
		needEdit=(EditText)findViewById(R.id.my_information_industry_edittext);//需求
		supplyEdit=(EditText)findViewById(R.id.my_information_supply_edittext);//供应
		
		init();
	}

	private void init() {
		titleRightBtn.setVisibility(View.GONE);
		rightBtn.setVisibility(View.VISIBLE);
		textTitle.setText("我的资料");
		titleLeftBtn.setText("返回");
		titleRightBtn.setText("编辑");
		//imgHead.setImageBitmap(BitmapLoad.loadBitmap(context, headImgPath));
		
		String name_Str =HSApplication.sp.getString("username","").toString();
		String gender_Str =HSApplication.sp.getString("gender","").toString();
		String company_Str =HSApplication.sp.getString("company","").toString();
		String zhiwu_Str =HSApplication.sp.getString("zhiwu","").toString();
		String occupation_Str =HSApplication.sp.getString("occupation","").toString();
		String need_Str =HSApplication.sp.getString("requirement","").toString();
		String supply_Str = HSApplication.sp.getString("supply","").toString();
		myName.setText((name_Str==null||"".equals(name_Str))?"":name_Str);
		sexText.setText((gender_Str==null||"".equals(gender_Str))?"":gender_Str);//性别
		companyText.setText((company_Str==null||"".equals(company_Str))?"":company_Str);//公司
		zhiwuText.setText((zhiwu_Str==null||"".equals(zhiwu_Str))?"":zhiwu_Str);//职务
		occupationText.setText((occupation_Str==null||"".equals(occupation_Str))?"":occupation_Str);//行业
		needText.setText((need_Str==null||"".equals(need_Str))?"":need_Str);//需求
		supplyText.setText((supply_Str==null||"".equals(supply_Str))?"":supply_Str);//供应
		imgHead=(ImageView)findViewById(R.id.my_information_head_img);
		headPath=HSApplication.sp.getString("imgHead", "");
		asyncLoadImage.imageLoader.displayImage(headPath, imgHead,asyncLoadImage.options);
	}
	
	private void setClick(){
		titleLeftBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//myMoveView.showHideLeftMenu();
				myMoveView.moveToLeft(true);
				MainService.isLeftMenu = true;
				finish();
			}
		});
		imgHead.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent headIntent = new Intent(context, SetHeadActivity.class);
				headIntent.putExtra("myId", myId);
				context.startActivity(headIntent);
			}
		});
		
		
		/*.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});*/
		
		sexRadio.setOnCheckedChangeListener(context);
		rightBtn.setOnCheckedChangeListener(context);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		
		if(isChecked){//编辑
			companyText.setVisibility(View.GONE);
			zhiwuText.setVisibility(View.GONE);
			occupationText.setVisibility(View.GONE);
			needText.setVisibility(View.GONE);
			supplyText.setVisibility(View.GONE);
			
			sexRadio.setVisibility(View.VISIBLE);
			companyEdit.setVisibility(View.VISIBLE);
			zhiwuEdit.setVisibility(View.VISIBLE);
			occupationEdit.setVisibility(View.VISIBLE);
			needEdit.setVisibility(View.VISIBLE);
			supplyEdit.setVisibility(View.VISIBLE);

			companyEdit.setText(companyText.getText().toString());
			zhiwuEdit.setText(zhiwuText.getText().toString());
			occupationEdit.setText(occupationText.getText().toString());
			needEdit.setText(needText.getText().toString());
			supplyEdit.setText(supplyText.getText().toString());
			
		}else{//完成   -- 更新
			sex=sexText.getText().toString();
			if("男".equals(sex)){
				sex_man.setChecked(true);
				sex_woman.setChecked(false);
			}else if("女".equals(sex)){
				sex_man.setChecked(false);
				sex_woman.setChecked(true);
			}else{
				sex_man.setChecked(false);
				sex_woman.setChecked(false);
			}
			company=companyEdit.getText().toString();
			zhiwu=zhiwuEdit.getText().toString();
			occupation=occupationEdit.getText().toString();
			need=needEdit.getText().toString();
			supply=supplyEdit.getText().toString();
			
			Communication.updateMyMaterial(this, myId,sex,company,zhiwu,occupation,need,supply);
			
			
		}
		
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		//获取变更后的选中项的ID
		int radioButtonId = group.getCheckedRadioButtonId();
		//根据ID获取RadioButton的实例
		RadioButton rb = (RadioButton)context.findViewById(radioButtonId);
		//更新文本内容，以符合选中项
		sexText.setText(rb.getText());
	}
	
	
	
	

}
